package com.finance.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidUtil {
	protected static final Logger logger = LoggerFactory.getLogger(ValidUtil.class);

	/**
	 * 手机验证支持虚拟运营商170号段
	 * @param phone
	 * @return
	 */
	public static boolean validPhone(String phone){
		if(StringUtils.isBlank(phone)) {
			return false;
		}
		Pattern p = Pattern.compile("^((13[0-9])|(14[57])|(15[^4,\\D])|(17[0,5-8])|(18[0-9]))\\d{8}$");  
		Matcher  m = p.matcher(phone);
		return m.matches();
	}
	/**
	 * 二代身份证号格式验证
	 * @param identityNo
	 * @return
	 */
	public static boolean validIdentityCard(String identityNo){
		if(StringUtils.isBlank(identityNo)) {
			return false;
		}
		Pattern p = Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$");  
		Matcher  m = p.matcher(identityNo);
		return m.matches();
	}
	/**
	 * email格式验证
	 * @param email
	 * @return
	 */
	public static boolean validEmail(String email){
		if(StringUtils.isBlank(email)) {
			return false;
		}
		Pattern p = Pattern.compile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");  
		Matcher  m = p.matcher(email);
		return m.matches();
	}
	/**
	 * date格式验证 yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static boolean validDate(String date){
		if(StringUtils.isBlank(date)) {
			return false;
		}
		Date dt =null;
		try {dt = DateUtil.str2date(date, "yyyy-MM-dd");
		} catch (ParseException e) {
 			logger.error(e.getMessage(),e);
		}
		if(dt==null) {
			return false;
		}
		return true;
	}
}
